﻿using AutoMapper;
using Domain.Entity;
using Domain.Entity.sys;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.CodeRuleService
{
    public class CodeRuleService : ICodeRuleService
    {
        private readonly ICodeRuleRepository codeRuleRepository;
        private readonly IMapper mapper;

        public CodeRuleService(ICodeRuleRepository codeRuleRepository,IMapper mapper)
        {
            this.codeRuleRepository = codeRuleRepository;
            this.mapper = mapper;
        }

        //添加编号规则信息
        public async Task<int> AddCodeRule(AddCodeRuleDto addCodeRuleDto)
        {
            //转换
            CodeRule codeRule = mapper.Map<CodeRule>(addCodeRuleDto);
            return await codeRuleRepository.AddCodeRule(codeRule);
        }

        //删除编号规则信息
        public async Task<int> DeleteCodeRule(int id)
        {
            return await codeRuleRepository.DeleteCodeRule(id);
        }

        //生成编号规则
        public async Task<string> GeneratedCode(int id)
        {
            return await codeRuleRepository.GeneratedCode(id);
        }


        //分页查询编号规则信息
        public async Task<FenYe<CodeRule>> GetCodeRule(FenYe<CodeRule> fenYe, string? name, string? prefix, string? timerule, string? starttime, string? endtime)
        {
            return await codeRuleRepository.GetCodeRule(fenYe,name,prefix,timerule,starttime,endtime);
        }

        //根据id查询编号规则信息
        public async Task<CodeRule> GetCodeRuleByid(int id)
        {
            return await codeRuleRepository.GetCodeRuleByid(id);
        }

        //修改编号规则信息
        public async Task<int> UpdateCodeRule(UpdateCodeRuleDto updateCodeRuleDto)
        {
            //转换
            CodeRule codeRule = mapper.Map<CodeRule>(updateCodeRuleDto);
            return await codeRuleRepository.UpdateCodeRule(codeRule);
        }
    }
}
